#!/bin/bash
#*****************************************************************************
#* Copyright (C) 2007 Ryan Mulder
#*
#* This program is free software; you can redistribute it and/or
#* modify it under the terms of the GNU General Public License
#* as published by the Free Software Foundation; either version 2
#* of the License, or (at your option) any later version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, write to the Free Software
#* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#*
#*****************************************************************************
# this function does the actual work of making the source package
# the version is passed to it as the first argument, below
function make_src_tarball
{
	set -e

	# root source dir
	basedir=.

	# create Makefiles
	$basedir/create_build_files4.sh --disable-mediactrl --rpath=/usr/lib/wxformbuilder

	# remove premake, as it is no longer necessary
	rm -rf $PREMAKE_DIR

	# remove scripts that execute premake, as they are no longer necessary
	rm -f $basedir/create_build_files*

	# Cleanup premake build-from-source files
	rm -rf $basedir/build/premake/4.3
	rm -rf $basedir/build/premake/bsd
	rm -rf $basedir/build/premake/linux
	rm -rf $basedir/build/premake/macosx
	rm -rf $basedir/build/premake/windows

	# use a temporary directory
	tmpdir=wxformbuilder-$1 #.orig

	cp -R $basedir   ../$tmpdir
	cd ..

	tar czf wxformbuilder_$1.orig.tar.gz $tmpdir
	rm -rf $tmpdir
}

changelog="output/Changelog.txt"

if [ ! -f $changelog ];
then
  echo "Sorry, could not find "$changelog". Need it to parse the version."
  exit 1
fi

cat "$changelog" |
while read line;
do
 version=`expr match "$line" '.*\([0-9]\.[0-9]\{1,2\}\.[0-9]\+\)'`
 if [ -n "$version" ]
 then
 echo "$version"
   # because I redirected cat to the while loop, bash spawned a subshell
   # this means "version" will go out of scope at the end of the loop
   # so I need to do everything here
   if [ ${#version} -ge 7 ]
   then
     version=${version/0/}
   fi
   make_src_tarball $version
   break
 fi
done

exit
